package com.uenpay.im.ui.old.activity.red;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PickRedInvokItem extends HttpInvokeItem {

	public class PickRedInvokItemResult extends HttpInvokeResult {
		public ReceiveRedEntity mReceiveRedEntity = new ReceiveRedEntity();
	}

	public PickRedInvokItemResult getOutput() {
		return (PickRedInvokItemResult) GetResultObject();
	}

	public PickRedInvokItem(String packetsid) {
		// http://api.aitiapp.com/RedPackets/pickPackets?packetsid=
		String url = APIConfiguration.getBaseUrl()
				+ "Purse/RedPackets/pickPackets?packetsid=" + packetsid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		PickRedInvokItemResult result = new PickRedInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject jsonObject = json.optJSONObject("data");
			if (jsonObject == null) {
				return result;
			}
			result.mReceiveRedEntity.pickAmount = jsonObject
					.optString("pickAmount");
			result.mReceiveRedEntity.isOpened = jsonObject.optInt("isOpened");
			JSONObject redPacketsInfoJson = jsonObject
					.optJSONObject("redPacketsInfo");
			if (redPacketsInfoJson != null) {
				result.mReceiveRedEntity.custId = redPacketsInfoJson
						.optString("custid");
				result.mReceiveRedEntity.chatid = redPacketsInfoJson
						.optLong("chatid");
				result.mReceiveRedEntity.tcustId = redPacketsInfoJson
						.optLong("tcustid");
				result.mReceiveRedEntity.nickName = redPacketsInfoJson
						.optString("nickname");
				result.mReceiveRedEntity.blessing = redPacketsInfoJson
						.optString("blessing");
				result.mReceiveRedEntity.amount = redPacketsInfoJson
						.optString("amount");
				result.mReceiveRedEntity.packetsid = redPacketsInfoJson
						.optString("packetsid");
				result.mReceiveRedEntity.number = redPacketsInfoJson
						.optInt("number");
				result.mReceiveRedEntity.opennumber = redPacketsInfoJson
						.optInt("opennumber");
				result.mReceiveRedEntity.leftPacketsNum = redPacketsInfoJson
						.optInt("leftnum");
				result.mReceiveRedEntity.expired = redPacketsInfoJson
						.optInt("expired");
				result.mReceiveRedEntity.spendtime = redPacketsInfoJson
						.optLong("spendtime");
				result.mReceiveRedEntity.type = redPacketsInfoJson
						.optInt("type");
				result.mReceiveRedEntity.createtime = redPacketsInfoJson
						.optString("createtime");
				result.mReceiveRedEntity.openamount = redPacketsInfoJson
						.optString("openamount");
			}

			JSONArray jsonArray = jsonObject.optJSONArray("packetsOpenList");
			if (jsonArray != null && jsonArray.length() > 0) {
				for (int i = 0, length = jsonArray.length(); i < length; i++) {
					result.mReceiveRedEntity.arrayList
							.add(desRedRecordEntity(jsonArray.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

	private RedRecordEntity desRedRecordEntity(JSONObject json) {
		RedRecordEntity mRedRecordEntity = new RedRecordEntity();
		mRedRecordEntity.custId = json.optString("custid");
		mRedRecordEntity.nickName = json.optString("nickname");
		mRedRecordEntity.recordMoney = json.optString("amount");
		mRedRecordEntity.isluck = json.optInt("isluck");
		mRedRecordEntity.recordTime = json.optString("time");
		mRedRecordEntity.thankswords = json.optString("thankswords");
		return mRedRecordEntity;
	}

}
